<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="公司" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <el-tree
          :data="data"
          :props="defaultProps"
          default-expand-all
          @node-click="handleNodeClick"
        >
          <template slot-scope="{ node, data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="人" /><span>{{ node.label }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="hAdd(data.id)"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="hEdit(data.id)"
                        >编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="hDel(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :title="isEdit ? '编辑部门' : '添加子部门'"
        :visible.sync="dialogVisible"
      >
        <!-- 弹框组件 -->
        <dept-dialog
          v-if="dialogVisible"
          :id="curId"
          :edit="isEdit"
          :origin-list="originList"
          @success="hSuccess"
          @close="hClose"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import deptDialog from './deptDialog.vue'

export default {
  components: { deptDialog },
  data() {
    return {
      // 保存企业的部门列表信息
      data: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      // 弹框的开关
      dialogVisible: false,
      // 保存父向子传的数据
      curId: '',
      // 判断是否编辑
      isEdit: false,
      // 校验需要的数据
      originList: []
    }
  },
  created() {
    // 查询企业的部门列表
    this.loadDepartments()
  },
  methods: {
    handleNodeClick(data) {
      console.log(data)
    },
    // 查询企业的部门列表
    async loadDepartments() {
      const res = await getDepartments()
      res.data.depts.shift()
      this.data = tranListToTreeData(res.data.depts)
      this.originList = res.data.depts.map((item) => {
        return { id: item.id, name: item.name, code: item.code, pid: item.pid }
      })
    },
    // 添加子部门
    hAdd(id) {
      // 开启弹框
      this.dialogVisible = true
      // 赋值
      this.curId = id
      // 判断点击的不是编辑
      this.isEdit = false
    },
    // 收到子组件中的确认事件
    hSuccess() {
      // 关闭弹框
      this.dialogVisible = false
      // 更新数据
      this.loadDepartments()
    },
    // 收到子组件中的取消事件
    hClose() {
      // 关闭弹框
      this.dialogVisible = false
    },
    // 编辑部门
    hEdit(id) {
      this.isEdit = true
      // 开启弹框
      this.dialogVisible = true
      // 赋值
      this.curId = id
    },
    // 删除部门
    hDel(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          // 确认
          // 删除请求
          await delDepartment(id)
          // 更新数据
          this.loadDepartments()
          // 提示
          this.$message.success('删除成功')
        })
        .catch(() => {
          // 取消
        })
    }
  }
}
</script>
